import type { toDoItem } from "@/types/todo";
import { acceptHMRUpdate, defineStore } from "pinia";
import { reactive, ref } from "vue";

export const useUserStore = defineStore('user', () => {
    const token = ref<string>("")
    const isAuthenticated = ref<boolean>(false)

    const setToken = (str: string = '') => {
        token.value = str
    }
    const setAuthenticated = (flag: boolean = false) => {
        isAuthenticated.value = flag
    }

    const clearStorage = () => {
        setToken()
        setAuthenticated()
    }

    return {
        isAuthenticated,
        token,
        setToken,
        setAuthenticated,
        clearStorage
    }
})
if (import.meta.hot) {
    import.meta.hot.accept(acceptHMRUpdate(useUserStore, import.meta.hot))
}

export const useToDoStore = defineStore('toDo', () => {
    let toDoList: Array<toDoItem> = reactive([])
    function addToDoItem(item: toDoItem) {
        toDoList.push(item)
    }
    function delToDoItem(item: toDoItem) {
        toDoList = toDoList.filter(i => i.toDoId !== item.toDoId)
    }
    return {
        toDoList,
        addToDoItem,
        delToDoItem
    }
})